<template>
  <div>
    <el-card>
      <el-form ref="form" :model="form" class="form" :inline="true" size="mini">
        <el-form-item label="店铺ID/昵称" class="name">
          <el-input v-model="form.shopName"></el-input>
        </el-form-item>
        <el-form-item label="用户ID/昵称" class="name">
          <el-input v-model="form.userName"></el-input>
        </el-form-item>
        <el-form-item label="交易类型">
          <el-select v-model="form.type" placeholder="请选择交易类型">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="提现单号" class="name">
          <el-input v-model="form.num"></el-input>
        </el-form-item>
        <el-form-item label="日期">
          <el-date-picker
            v-model="applyTime"
            type="daterange"
            value-format="timestamp"
            :clearable="false"
            unlink-panels
            range-separator="至"
            start-placeholder="开始日期"
            @change="change"
            end-placeholder="结束日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" size="small" @click="search"
            >查询</el-button
          >
          <el-button size="small" @click="resetForm ">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <h4>
      <div>
        <span class="word">店铺提现金额:</span><span class="money">{{shopOrder}}元</span>
        <span class="word">用户提现金额:</span><span class="money">{{userOrder}}元</span>
        <span class="word">总提现金额:</span><span class="money">{{total}}元</span>
      </div>
    </h4>
    <el-table :data="tableData" row-key="id" class="customer-table">
      <el-table-column label="交易类型" prop="type" align="center" />
      <el-table-column label="店铺ID/用户ID" prop="id" align="center" />
      <el-table-column label="店铺昵称/用户昵称" prop="name" align="center">
        <template slot-scope="{ row }">
          <span>{{
            row.shopName ? `${row.shopName}` : `${row.userName}`
          }}</span>
        </template>
      </el-table-column>
      <el-table-column label="提现单号" prop="num" align="center" />
      <el-table-column label="提现金额" align="center" >
        <template slot-scope="{ row }">
          <span>{{ row.money }} 元</span>
        </template>
      </el-table-column>
      <el-table-column label="申请提现时间" prop="applyTime" align="center" >
          <template slot-scope="{ row }">
          <span>{{ row.applyTime | date }}</span>
        </template>
      </el-table-column>
    </el-table>
     <el-row type="flex" justify="center" align="middle" style="height: 60px">
        <el-pagination
           :current-page="page.page"
            :page-size="page.size"
            :total="page.total"
            :page-sizes="[2, 3, 5, 10]"
            layout="total, sizes, prev, pager, next, jumper"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
        >
        </el-pagination>
      </el-row>
  </div>
</template>

<script>
import moment from 'moment'
import { getExpensesDetail } from '@/api/expensesDetail.js'
import { queryFn } from '@/utils' // 多选项查询
export default {
  data () {
    return {
      page: {
        page: 1,
        size: 5,
        total: 7
      },
      options: [
        {
          value: '店铺提现',
          label: '店铺提现'
        },
        {
          value: '用户提现',
          label: '用户提现'
        }
      ],
      applyTime: '',
      form: {
        shopName: '',
        userName: '',
        type: '',
        num: '',
        applyTime_gte: '',
        applyTime_lte: ''
      },
      tableData: []
    }
  },
  created () {
    this.getExpensesDetail()
  },
  computed: {
    // 店铺提现金额
    shopOrder () {
      const res = this.tableData.filter((obj) => {
        return obj.type === '店铺提现'
      })
      return res.reduce((sum, obj) => sum + obj.money, 0)
    },
    // 用户提现金额
    userOrder () {
      const res = this.tableData.filter((obj) => {
        return obj.type === '用户提现'
      })
      return res.reduce((sum, obj) => sum + obj.money, 0)
    },
    // 用户提现金额
    total () {
      return this.tableData.reduce((sum, obj) => sum + obj.money, 0)
    }
  },
  filters: {
    date: function (value) {
      if (!value) return ''
      return moment(value).format('YYYY-MM-DD HH:mm:ss')
    }
  },
  methods: {
    // 获取日期
    change (v) {
      // console.log(v)
      if (!v) return
      this.form.applyTime_gte = v[0]
      this.form.applyTime_lte = v[1]
    },
    async getExpensesDetail () {
      const { data } = await getExpensesDetail()
      this.tableData = data
    },
    // 查询
    async search () {
      const obj = queryFn(this.form)
      console.log(obj)
      const { data } = await getExpensesDetail(obj)
      this.tableData = data
      this.page.total = this.tableData.length
      console.log(data)
      this.form.shopName = ''
      this.form.type = ''
      this.form.userName = ''
      this.form.num = ''
      this.form.applyTime_gte = ''
      this.form.applyTime_lte = ''
      this.applyTime = ''
    },
    // 重置
    resetForm () {
      this.form.shopName = ''
      this.form.type = ''
      this.form.userName = ''
      this.form.num = ''
      this.form.applyTime_gte = ''
      this.form.applyTime_lte = ''
      this.applyTime = ''
      this.getExpensesDetail()
    },
    // 分页
    async  handleSizeChange (newsize) {
      this.page.size = newsize
      const { data } = await getExpensesDetail({
        _page: this.page.page,
        _limit: this.page.size
      })
      this.tableData = data
    },
    async  handleCurrentChange (newpage) {
      this.page.page = newpage
      // console.log(newpage)
      const { data } = await getExpensesDetail({
        _page: this.page.page,
        _limit: this.page.size
      })
      this.tableData = data
    }
  }
}
</script>

<style lang="less" scoped>
// .remark {
//   width: 1000px;
//   height: 1000px;
// }
// .el-card {
//   // margin-bottom: 30px;
// }
.form {
  box-sizing: border-box;
}
/deep/.customer-table th {
  border: none;
  // background-color: rgb(170, 56, 56);
  // color: white;
  // border-radius: 5px;
}
.customer-table /deep/ th.el-table__cell.is-leaf {
  // background-color: rgb(233, 237, 250);

  border: none;
}
.customer-table /deep/ .el-table__row > td {
  border: none;
}
.customer-table /deep/ .el-table__row > td {
  border: none;
}
.el-button + .el-button {
  margin-left: 30px;
}
.el-form--inline .el-form-item {
  margin-right: 120px;
}
.customer-table th {
  border: none;
}
.customer-table td,
.customer-table th.is-leaf {
  border: none;
}
// 表格最外边框
.el-table--border,
.el-table--group {
  border: none;
}
// 头部边框
.customer-table thead tr th.is-leaf {
  border: 1px solid #ebeef5;
  border-right: none;
}
.customer-table thead tr th:nth-last-of-type(2) {
  border-right: 1px solid #ebeef5;
}
// 表格最外层边框-底部边框
.el-table--border::after,
.el-table--group::after {
  width: 0;
}
.customer-table::before {
  width: 0;
}
.customer-table .el-table__fixed-right::before,
.el-table__fixed::before {
  width: 0;
}
// 表格有滚动时表格头边框
.el-table--border th.gutter:last-of-type {
  border: 1px solid #ebeef5;
  border-left: none;
}
.word {
  margin-right: 10px;
}
.money {
  margin-right: 30px;
  color: red;
}
</style>
